import { Button, Icon, Toast } from 'react-vant'
import { useState } from 'react'
import { likedArticle, deleteLiked } from '../api/article'
export default (porps) => {
    const [loading, setLoading] = useState(false)
    const [attitude, setAttitude] = useState(porps.attitude)
    const attitudeClick = async () => {
        setLoading(true)
        try {
            if (attitude === 1) {
                const res = await deleteLiked(porps.art_id)
                setAttitude(-1)
                Toast('取消点赞成功')
            } else {
                const res = await likedArticle(porps.art_id)
                setAttitude(1)
                Toast('点赞成功')
            }
        } catch (error) {

        }
        setLoading(false)
    }
    if (attitude === -1) {
        return (<Button type="default" onClick={attitudeClick} plain loading={loading} round><Icon name="good-job-o" /></Button>)
    } else {
        return (<Button type="default" onClick={attitudeClick} plain loading={loading} round><Icon name="good-job" className="liked" /></Button>)
    }
}